$(function () {
    $("#sendBtn").click(send_letter);
    $(".closeMessage").click(delete_msg);
});

function send_letter() {
    $("#sendModal").modal("hide");

    var toName = $("#recipient-name").val();
    var content = $("#message-text").val();

    $.ajax(CONTEXT_PATH + "/letter/send", {
        method: 'POST',
        data: {toName: toName, content: content},
        success: function (result) {
            result = $.parseJSON(result);
            if (result.code === 0) {
                $("#hintBody").text("发送成功");
            } else {
                $("#hintBody").text(result.msg);
            }

            $("#hintModal").modal("show");
            setTimeout(function () {
                $("#hintModal").modal("hide");
                location.reload();
            }, 2000);
        },
        error: function () {
            alert("请检查网络！");
        }
    });
}

function delete_msg() {
    // TODO 删除数据
    var btn = this;
    var id = $(btn).prev().val();
    $.ajax(CONTEXT_PATH + "/letter/delete",{
        method: "POST",
        data:{"id":id},
        success: function (result){
            result = $.parseJSON(result);
            if(result.code==0){
                $(btn).parents(".media").remove();
            }else {
                alert(data.msg);
            }
        }
    });
}